﻿@model IEntityEditModel
@{
    ViewBag.Title = (Model.Item.IsNewCreated ? "Create " : "Edit ") + Model.Metadata.Name;
    object area = ViewContext.RouteData.Values["Area"];
    if (area == null)
    {
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    else
    {
        Layout = "~/Areas/" + area.ToString() + "/Views/Shared/_Layout.cshtml";
    }
}
<div class="modal fade" id="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="dialogTitle">保存结果</h4>
            </div>
            <div class="modal-body" id="dialogContent">

            </div>
            <div class="modal-footer">
                <button id="dialogOK" type="button" class="btn btn-primary">确定</button>
                <button id="dialogCancel" type="button" class="btn btn-default">取消</button>
            </div>
        </div>
    </div>
</div>
<ul class="breadcrumb">
    <li><a href="@Url.Action("Index", "Home")">首页</a></li>
    <li><a href="@Html.Raw(Url.Action("Index"))">@Model.Metadata.Name</a></li>
    <li class="active">@(Model.Item.IsNewCreated ? "新建" : "编辑")</li>
</ul>
<form id="form" class="form-horizontal" role="form" action="@Url.Action("Update")" method="post" enctype="multipart/form-data">
    @foreach (var property in Model.Properties)
    {
        <div class="form-group">
            <label class="col-sm-2 control-label">@property.Name</label>
            <div class="col-sm-10">
                @Html.Editor(Model.Item, property, property.GetValue(Model.Item))
            </div>
        </div>
    }
    <div class="control-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="button" class="btn btn-primary" onclick="gotoSubmit();">保存</button>
            <a href="@Html.Raw(Url.Action("Index"))" class="btn btn-default">返回</a>
            <input name="id" type="hidden" value="@Model.Item.Index" />
        </div>
    </div>
</form>
@*<script src="~/Scripts/jstree.js"></script>*@
@*<script type="text/javascript" src="~/Scripts/jquery.form.js"></script>*@
<script type="text/javascript">
    function dialog(title, content, ok) {
        $('#dialogTitle').html(title);
        $('#dialogContent').html(content);
        $('#dialog').modal('show');
        $('#dialogOK').unbind('click');
        if (ok == undefined)
            $('#dialogOK').click(function () {
                $('#dialog').modal('hide');
            });
        else
            $('#dialogOK').click(ok);
    }
    $('#dialogCancel').click(function () {
        $('#dialog').modal('hide');
    });
    $('#dialog').modal({ show: false });

    function gotoSubmit() {
        $.ajax({
            url: '@Url.Action("Update")',
            dataType: 'json',
            type: 'POST',
            data: new FormData($('#form')[0]),
            processData: false,
            contentType: false,
            error: function (result) {
                if (result.statusCode == '500') {
                    dialog('保存结果', '<p>保存失败. 原因：服务器错误。</p>', function () { $('#dialog').modal('hide'); });
                    return;
                }

                result.responseJSON
                dialog('保存结果', '<p>保存失败. 原因：' + result.responseText + '。</p>', function () { $('#dialog').modal('hide'); });
            },
            success: function (result) {
                dialog('保存结果', '<p>保存成功,是否返回视图列表.</p>', function () { window.location = '@Url.Action("Index")'; });
            }
        });
    }
</script>